package com.lzxuni.modules.dataAcquisition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.entity.ChainLink;
import com.lzxuni.modules.dataAcquisition.entity.ChainLinkProcess;
import com.lzxuni.modules.dataAcquisition.entity.ChainLinkProcessIndex;
import com.lzxuni.modules.dataAcquisition.mapper.ChainLinkMapper;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessIndexService;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessService;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 追溯链条-环节 服务实现类
 * </p>
 *
 * @author wzx
 * @since 2019-06-25
 */
@Service
public class ChainLinkServiceImpl extends ServiceImpl<ChainLinkMapper, ChainLink> implements ChainLinkService {

    @Autowired
    private ChainLinkMapper chainLinkMapper;

    @Autowired
    private ChainService chainService;

    @Autowired
    private ChainLinkProcessService chainLinkProcessService;

    @Autowired
    private ChainLinkProcessIndexService chainLinkProcessIndexService;

    @Override
    public PageInfo<ChainLink> queryPage(PageParameter pageParameter, ChainLink chainLink) throws SQLException {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<ChainLink> list;
        if (StringUtils.isNotEmpty(chainLink.getLinkName()) && StringUtils.isEmpty(chainLink.getChainId())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().like("link_name", chainLink.getLinkName()));
        }else if(StringUtils.isNotEmpty(chainLink.getChainId()) && StringUtils.isEmpty(chainLink.getLinkName())){
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().eq("chain_id", chainLink.getChainId()));
        } else if (StringUtils.isNotEmpty(chainLink.getLinkName()) && StringUtils.isNotEmpty(chainLink.getChainId())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().like("link_name", chainLink.getLinkName()).eq("chain_id", chainLink.getChainId()));
        } else {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>());
        }
        if (list != null && list.size() > 0) {
            for (ChainLink chainLink1 : list) {
                Chain chain = chainService.getById(chainLink1.getChainId());
                chainLink1.setChainName(chain == null ? null : chain.getChainName());
            }
        }
        PageInfo<ChainLink> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Map<String, Object>> queryPage(ChainLink chainLink) throws SQLException {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<ChainLink> list;
        if (StringUtils.isNotEmpty(chainLink.getLinkName()) && StringUtils.isEmpty(chainLink.getParentId())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().like("link_name", chainLink.getLinkName()).orderByAsc("create_time"));
        }else if(StringUtils.isNotEmpty(chainLink.getParentId()) && StringUtils.isEmpty(chainLink.getLinkName())){
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().eq("chain_id", chainLink.getParentId()).orderByAsc("create_time"));
        } else if (StringUtils.isNotEmpty(chainLink.getLinkName()) && StringUtils.isNotEmpty(chainLink.getParentId())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().like("link_name", chainLink.getLinkName()).eq("chain_id", chainLink.getParentId()).orderByAsc("create_time"));
        } else {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().orderByAsc("create_time"));
        }
        if (list != null && list.size() > 0) {
            for (ChainLink chainLink1 : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", chainLink1.getId());
                map.put("parentId", "");
                map.put("name", chainLink1.getLinkName());
                map.put("linkName", chainLink1.getLinkName());
                map.put("code", chainLink1.getLinkCode());
                map.put("linkCode", chainLink1.getLinkCode());
                map.put("industryCategory", chainLink1.getIndustryCategory());
                map.put("cyclicAcquisition", null);
                map.put("level", 0);
                mapList.add(map);

                List<ChainLinkProcess> processes =chainLinkProcessService.list(new QueryWrapper<ChainLinkProcess>().eq("link_id",chainLink1.getId()).orderByAsc("create_time"));
                if (processes != null && processes.size() > 0) {
                    for (ChainLinkProcess process : processes) {
                        Map<String, Object> map1 = new HashMap<>();
                        map1.put("id", process.getId());
                        map1.put("parentId", process.getParentId());
                        map1.put("name", process.getProcessName());
                        map1.put("code", process.getProcessCode());
                        map1.put("processType", process.getProcessType());
                        map1.put("remark", process.getRemark());
                        map1.put("cyclicAcquisition", null);
                        map1.put("level", 1);
                        mapList.add(map1);

                        List<ChainLinkProcessIndex> indices = chainLinkProcessIndexService.list(new QueryWrapper<ChainLinkProcessIndex>().eq("process_id",process.getId()).orderByAsc("create_time"));
                        if (indices != null && indices.size() > 0) {
                            for (ChainLinkProcessIndex index : indices) {
                                Map<String, Object> map2 = new HashMap<>();
                                map2.put("id", index.getId());
                                map2.put("parentId", index.getParentId());
                                map2.put("name", index.getIndexName());
                                map2.put("code", index.getIndexCode());
                                map2.put("unit", index.getUnit());
                                map2.put("indexType", index.getIndexType());
                                map2.put("collectionType", index.getCollectionType());
                                map2.put("collectionMode", index.getCollectionMode());
                                map2.put("cyclicAcquisition", index.getCyclicAcquisition());
                                map2.put("remark", index.getRemark());
                                map2.put("level", 2);
                                mapList.add(map2);
                            }
                        }
                    }
                }
            }

        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> getTree(ChainLink chainLink) throws SQLException {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<ChainLink> list;
        if (StringUtils.isNotEmpty(chainLink.getLinkName()) && StringUtils.isEmpty(chainLink.getParentId())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().like("link_name", chainLink.getLinkName()));
        }else if(StringUtils.isNotEmpty(chainLink.getParentId()) && StringUtils.isEmpty(chainLink.getLinkName())){
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().eq("chain_id", chainLink.getParentId()));
        } else if (StringUtils.isNotEmpty(chainLink.getLinkName()) && StringUtils.isNotEmpty(chainLink.getParentId())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>().like("link_name", chainLink.getLinkName()).eq("chain_id", chainLink.getParentId()));
        } else {
            list = baseMapper.selectList(new QueryWrapper<ChainLink>());
        }
        if (list != null && list.size() > 0) {
            for (ChainLink chainLink1 : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", chainLink1.getId());
                map.put("pId", "-1");
                map.put("name", chainLink1.getLinkName());
                map.put("open", "false");
                map.put("isParent", "true");
                map.put("checked", false);
                mapList.add(map);

                List<ChainLinkProcess> processes =chainLinkProcessService.list(new QueryWrapper<ChainLinkProcess>().eq("link_id",chainLink1.getId()));
                if (processes != null && processes.size() > 0) {
                    for (ChainLinkProcess process : processes) {
                        Map<String, Object> map1 = new HashMap<>();
                        map1.put("id", process.getId());
                        map1.put("pId", process.getParentId());
                        map1.put("name", process.getProcessName());
                        map1.put("open", "false");
                        map1.put("isParent", "false");
                        map1.put("checked", false);
                        mapList.add(map1);

                        List<ChainLinkProcessIndex> indices = chainLinkProcessIndexService.list(new QueryWrapper<ChainLinkProcessIndex>().eq("process_id",process.getId()));
                        if (indices != null && indices.size() > 0) {
                            for (ChainLinkProcessIndex index : indices) {
                                Map<String, Object> map2 = new HashMap<>();
                                map2.put("id", index.getId());
                                map2.put("pId", index.getParentId());
                                map2.put("name", index.getIndexName());
                                map2.put("open", "false");
                                map2.put("isParent", "false");
                                map2.put("checked", false);
                                mapList.add(map2);
                            }
                        }
                    }
                }
            }

        }
        return mapList;
    }

    @Override
    public List<Tree> getTree(String chainId) {
        chainId = StringUtils.isNotEmpty(chainId) ? chainId : "0";
        //定义返回对象
        List<Tree> list = new ArrayList<>();
        List<ChainLink> chainLinks = baseMapper.selectList(new QueryWrapper<ChainLink>().eq("chain_id", chainId));
        if (chainLinks != null && chainLinks.size() > 0) {
            for (ChainLink chainLink : chainLinks) {
                Tree tree = new Tree();
                tree.setId(chainLink.getId());
                tree.setParentId("0");
                tree.setText(chainLink.getLinkName());
                tree.setTitle(chainLink.getLinkName());
                tree.setValue(chainLink.getLinkName());
                tree.setIcon(null);
                tree.setShowcheck(false);
                tree.setCheckstate(0);
                tree.setComplete(true);

                List<ChainLinkProcess> processes =chainLinkProcessService.list(new QueryWrapper<ChainLinkProcess>().eq("link_id",chainLink.getId()));
                if (processes != null && processes.size() > 0) {
                    List<Tree> list1 = new ArrayList<>();
                    for (ChainLinkProcess process : processes) {
                        Tree tree1 = new Tree();
                        tree1.setId(process.getId());
                        tree1.setParentId(process.getParentId());
                        tree1.setText(process.getProcessName());
                        tree1.setTitle(process.getProcessName());
                        tree1.setValue(process.getProcessName());
                        tree1.setIcon(null);
                        tree1.setShowcheck(false);
                        tree1.setCheckstate(0);
                        tree1.setComplete(true);


                        List<ChainLinkProcessIndex> indices = chainLinkProcessIndexService.list(new QueryWrapper<ChainLinkProcessIndex>().eq("process_id",process.getId()));
                        if (indices != null && indices.size() > 0) {
                            List<Tree> list2 = new ArrayList<>();
                            for (ChainLinkProcessIndex index : indices) {
                                Tree tree2 = new Tree();
                                tree2.setId(index.getId());
                                tree2.setParentId(index.getParentId());
                                tree2.setText(index.getIndexName());
                                tree2.setTitle(index.getIndexName());
                                tree2.setValue(index.getIndexName());
                                tree2.setIcon(null);
                                tree2.setShowcheck(false);
                                tree2.setCheckstate(0);
                                tree2.setComplete(true);
                                list2.add(tree2);

                                //list.add(tree2);
                            }
                            tree1.setChildNodes(list2);
                            tree1.setHasChildren(true);
                            tree1.setIsexpand(true);
                        }
                        list1.add(tree1);
                        //list.add(tree1);
                    }
                    tree.setChildNodes(list1);
                    tree.setHasChildren(true);
                    tree.setIsexpand(true);
                }
                list.add(tree);
            }
        }
        return list;
    }

    @Override
    public List<Tree> getTreeSelectTree(String nature) {
        List<Tree> list = new ArrayList<>();
        List<ChainLink> chainLinks = baseMapper.selectList(new QueryWrapper<>());
        if (chainLinks != null && chainLinks.size() > 0) {
            for (ChainLink chainLink : chainLinks) {
                Tree tree = new Tree();
                tree.setId(chainLink.getId());
                tree.setParentId("");
                tree.setText(chainLink.getLinkName());
                tree.setTitle(chainLink.getLinkName());
                tree.setShowcheck(false);
                tree.setCheckstate(0);
                tree.setComplete(true);
                list.add(tree);
            }
        }
        return list;
    }
}
